﻿<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>会员管理<small>会员档案</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>会员管理</el-breadcrumb-item>
            <el-breadcrumb-item>会员档案</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="姓名/手机号" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="pagination.currentPage=1;findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" border stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="fileNumber" label="档案号" align="center"></el-table-column>
                <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                <!--<el-table-column prop="email" label="邮箱" align="center"></el-table-column>-->
                <!--<el-table-column label="邮箱" align="center">
                    <template slot-scope="scope">
                        <a style="color:deepskyblue;" href="mailto:'+scope.row.email+'">{{scope.row.email}}</a>
                    </template>
                </el-table-column>-->
                <el-table-column label="性别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="idCard" label="身份证" align="center"></el-table-column>
                <el-table-column prop="phoneNumber" label="手机号码" align="center"></el-table-column>
                <el-table-column prop="birthday" label="生日" align="center"></el-table-column>
                <el-table-column prop="regTime" label="注册时间" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-sizes="[10, 20, 30, 40, 50]"
                        :page-size="pagination.pageSize"
                        layout="total,sizes, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 添加标签弹层-->
            <div class="add-form">
                <el-dialog title="添加会员档案" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="11">
                                <el-form-item label="档案号" prop="fileNumber">
                                    <el-input v-model="formData.fileNumber" maxlength="15"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="11">
                                <el-form-item label="姓名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item label="性别" prop="sex">
                                    <el-select v-model="formData.sex">
                                        <el-option label="男" value="1"></el-option>
                                        <el-option label="女" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="10">
                                <el-form-item label="生日" prop="birthday">
                                    <el-date-picker
                                            v-model="formData.birthday"
                                            type="date"
                                            :picker-options="pickerOptions"
                                            placeholder="选择日期">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item label="手机号" prop="phoneNumber">
                                    <el-input v-model="formData.phoneNumber" maxlength="11"/>
                                </el-form-item>
                            </el-col>
                            <!--<el-col :span="10">
                                <el-form-item label="邮箱" prop="email">
                                    <el-input v-model="formData.email"/>
                                </el-form-item>
                            </el-col>-->
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item label="身份证" prop="idCard">
                                    <el-input v-model="formData.idCard" maxlength="18"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="10">
                                <el-form-item label="备注" prop="remark">
                                    <el-input v-model="formData.remark"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button @click="resetForm('dataAddForm')">重置</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑会员档案" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="11">
                                <el-form-item label="档案号" prop="fileNumber">
                                    <el-input v-model="formData.fileNumber" disabled/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="10">
                                <el-form-item label="姓名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item label="性别" prop="sex">
                                    <el-select v-model="formData.sex">
                                        <el-option label="男" value="1"></el-option>
                                        <el-option label="女" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="生日" prop="birthday">
                                    <el-date-picker
                                            v-model="formData.birthday"
                                            type="date"
                                            :picker-options="pickerOptions"
                                            placeholder="选择日期">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="18">
                                <el-form-item label="身份证" prop="idCard">
                                    <el-input v-model="formData.idCard" maxlength="18"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item label="手机号" prop="phoneNumber">
                                    <el-input v-model="formData.phoneNumber" maxlength="11"/>
                                </el-form-item>
                            </el-col>
                            <!--<el-col :span="10">
                                <el-form-item label="邮箱" prop="email">
                                    <el-input v-model="formData.email"/>
                                </el-form-item>
                            </el-col>-->
                        </el-row>
                        <el-row>
                            <el-col :span="11">
                                <el-form-item label="注册时间">
                                    <el-input v-model="formData.regTime" disabled/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="10">
                                <el-form-item label="备注" prop="remark">
                                    <el-input v-model="formData.remark"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script type="text/javascript">
    var Main = {
        data() {
            //唯一性验证 手机号 邮箱 档案号 身份证号
            let phoneValidate = (rule, value, callback) => {
                //通过axios查询后台数据
                axios.post('/member/phoneValidator.do', this.formData).then((res) => {
                    if (res.data === '手机号已经存在') {
                        return callback(new Error('手机号已经存在'));
                    } else {
                        callback();
                    }
                })
            };
            let fileNumberValidate = (rule, value, callback) => {
                //通过axios查询后台数据
                axios.post('/member/fileNumberValidator.do', this.formData).then((res) => {
                    if (res.data === '档案号已经存在') {
                        return callback(new Error('档案号已经存在'));
                    } else {
                        callback();
                    }
                })
            };
            let idCardValidate = (rule, value, callback) => {
                //通过axios查询后台数据
                axios.post('/member/idCardValidator.do', this.formData).then((res) => {
                    if (res.data === '身份证已经存在') {
                        return callback(new Error('身份证已经存在'));
                    } else {
                        callback();
                    }
                })
            };
            return {
                pagination: {//分页相关属性
                    currentPage: 1,  //当前页
                    pageSize: 10,  //一页显示的数量
                    total: 0,  //总数
                    queryString: null
                },
                dataList: [],//列表数据
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now() - 8.64e7;
                    }
                },
                formData: {
                    sex: '不限'
                },//表单数据
                tableData: [],//添加表单窗口中检查组列表数据
                dialogFormVisible: false,//控制增加窗口显示/隐藏
                dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
                dataAddForm: {
                    name: '',
                    phoneNumber: '',
                    fileNumber: '',
                    idCard: '',
                    email: '',
                    sex: [],
                    birthday: ''
                },
                dataEditForm: {
                    name: '',
                    phoneNumber: '',
                    idCard: '',
                    email: '',
                    sex: [],
                },
                rules: {//校验规则
                    //姓名验证
                    name: [{required: true, message: '姓名为必填项', trigger: 'blur'},
                        {pattern: /^[\u4E00-\u9FA5]{2,4}$/, message: '您的姓名有点小别致哦(#^.^#)'}],
                    //手机号验证
                    phoneNumber: [{required: true, message: '手机号为必填项', trigger: 'blur'},
                        {pattern: /^[1][3,4,5,7,8][0-9]{9}$/, message: '手机号格式错误', trigger: 'blur'},
                        {validator: phoneValidate, trigger: 'blur'}
                    ],
                    //档案号验证
                    fileNumber: [{required: true, message: '档案号为必填项', trigger: 'blur'},
                        {pattern: /^\d{11}|\d{15}$/, message: '档案号只能是数字并且不能少于11位多于15位', trigger: 'blur'},
                        {validator: fileNumberValidate, trigger: 'blur'}],
                    //身份证验证
                    idCard: [{required: true, message: '身份证为必填项', trigger: 'blur'},
                        {
                            pattern: /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i,
                            message: '身份证格式错误了,您应该不是外星人吧',
                            trigger: 'blur'
                        },
                        {validator: idCardValidate, trigger: 'blur'}],
                    //性别验证
                    sex: [{required: true, message: '请选择性别', trigger: 'change'}],
                    //日期验证
                    birthday: [{required: true, message: '请选择生日', trigger: 'change'}]
                },
            }
        },
        created() {
            this.findPage();
        },
        methods: {
            //VUE日期转换
            dateFormat: function (time) {
                var date = new Date(time);
                var year = date.getFullYear();
                /* 在日期格式中，月份是从0开始的，因此要加0
                 * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
                 * */
                var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                // 拼接
                return year + "-" + month + "-" + day;
            },
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },
            //弹出添加页面
            handleCreate() {
                this.resetForm('dataAddForm');
                this.dialogFormVisible = true;
            },
            //添加
            handleAdd() {
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        axios.post('/member/add.do', this.formData).then((res) => {
                            this.dialogFormVisible = false;
                            if (res.data.flag) {
                                this.$message.success(res.data.message);
                                /*//跳转到最后一页
                                this.handleCurrentChange((this.pagination.total / this.pagination.pageSize) + 1);*/
                            } else {
                                this.$message.error(res.data.message);
                            }
                        }).finally(()=> {
                            this.findPage();
                        });
                    } else {
                        //校验不通过，提示错误信息1
                        this.$message.error("表单数据校验失败，请检查输入是否正确！");
                        return false;
                    }
                });
            },
            //弹出编辑页面
            handleUpdate(row) {
                this.resetForm('dataEditForm');
                this.dialogFormVisible4Edit = true;
                axios.get('/member/findById.do?id=' + row.id).then((res) => {
                    if (res.data.flag) {
                        // res.data.data.regTime = this.dateFormat(res.data.data.regTime);
                        this.formData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            //编辑
            handleEdit() {
                this.$refs['dataEditForm'].validate((valid) => {
                    if (valid) {
                        axios.post('/member/edit.do', this.formData).then((res) => {
                            if (res.data.flag) {
                                this.$message.success(res.data.message);
                            } else {
                                this.$message.error(res.data.message);
                            }
                        }).catch((res) => {
                            this.showMessage(res);
                        }).finally(() => {
                            //不管成功还是失败，都调用分页查询方法
                            this.findPage();
                            //隐藏编辑窗口
                            this.dialogFormVisible4Edit = false;
                        });
                    } else {
                        //校验不通过，提示错误信息
                        this.$message.error("表单数据校验失败，请检查输入是否正确！");
                        return false;
                    }
                });
            },
            //分页查询
            //分页查询
            findPage() {
                //分页参数
                var param = {
                    currentPage: this.pagination.currentPage,//页码
                    pageSize: this.pagination.pageSize,//每页显示的记录数
                    queryString: this.pagination.queryString//查询条件
                };
                //请求后台
                axios.post("/member/findPage.do", param).then((response) => {
                    //为模型数据赋值，基于VUE的双向绑定展示到页面
                    this.dataList = response.data.rows;
                    this.pagination.total = response.data.total;
                });
            },
            // 重置表单
            resetForm(formName) {
                this.formData = {};
                if (this.$refs[formName] !== undefined) {
                    this.$refs[formName].resetFields()
                }
            },
            // 初始页currentPage、初始每页数据数pagesize和数据data
            handleSizeChange(size) {
                this.pagination.pageSize = size;
                this.findPage();
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;//指定最新的页码
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除该检查项, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//点击确认 - 执行
                    axios.get("/member/delete.do?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            this.$message.success(res.data.message);
                        } else {
                            //执行失败
                            this.$message.error(res.data.message);
                        }
                    }).catch((res) => {
                        this.showMessage(res);
                    }).finally(() => {
                        //不管成功还是失败，都调用分页查询方法
                        this.findPage();
                    });
                }).catch(() => {//点击取消 - 执行
                    this.$message({
                        type: 'info',
                        message: '操作已取消'
                    });
                });
            }
        }
    }
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')

</script>
</html>
